﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using HPCERP.Model;

namespace HPCERP.Service
{
    
    public class PhraseService : IPhraseService
    {


        #region IPhraseService Members

   
        public string GetNextPhrase()
        {
            try
            {
                hpc_erpEntities ctx = new hpc_erpEntities();
                int maxItem = GetTotalPhrases();
                Random rnd = new Random();
                int id;
                string phraseText = "";
                while (phraseText.Equals(""))
                {
                    id = rnd.Next(maxItem);
                    phraseText = (from p in ctx.Phrase where p.Id_Phrase == id select p.PhraseText).FirstOrDefault();
                }
                return phraseText;
            }
            catch (Exception ex)
            {
                LogMessage.WriteMessage(ex.Message);
                return null;
            }
        }

        private int GetTotalPhrases()
        {
            hpc_erpEntities ctx = new hpc_erpEntities();
            var phrases = from p in ctx.Phrase orderby p.Id_Phrase descending select p.Id_Phrase;
            return phrases.FirstOrDefault();
        }

        #endregion
    }
}
